﻿#include "stdio.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <windows.h>
#include <boost/algorithm/string.hpp>
#include <BaseCommon.h>
#include "swi_parameter_check.h"

namespace Swi
{

    ParameterCheck::ParameterCheck()
    {
    }

    ParameterCheck::~ParameterCheck()
    {
    }

    void ParameterCheck::ParameterCheckByDat(Swi::SwiFile swi_file, Dat::DatFile dat_file)
    {
        BPA::BPAFilesIO::GenUnitDataVector GenUnitDataVectorList;
        GenUnitDataVectorList = GetGenUnitDataVectorList();

        DATGenCheckInfo.clear();
        DATLineCheckInfo.clear();
        CommonGenCapCheckInfo.clear();
        GenUnitDataVectorList.clear();
        char DATRemark_UnComplet[50] = "DAT容量信息未填";
        char SWIRemark_UnComplet[50] = "SWI容量信息未填";
        char DAT_SWIRemark_UnComplet[50] = "SWI和DAT容量信息均未填";
        char DATRemark_NotExist[50] = "替代";
        char SWIRemark[50] = "注释掉";
        char GenType[10] = "发电机";
        char ExistFlag[5] = "是";
        char NotExistFlag[5] = "否";
        char CapDiff[50] = "容量不一致";
        char CapSame[50] = "容量一致";
        char Miss[50] = "空缺";
        int DATLineID = 0;
        char buf[2000];
        SqliteHelperDataBase db;
        SqliteHelperBuffer bufSQL;
        db.open(DB_NAME);
        if ((!db.tableExists("SWIChekCap")))
            db.execDML("create table SWIChekCap(CaseID Integer,CodeID Integer,Vol REAL,GenName TEXT,DATCap TEXT,SWICap TEXT,CapDelt TEXT,SWILine int,Remark TEXT);");
        db.execDML("delete from SWIChekCap");
        if ((!db.tableExists("SWIChekLost")))
            db.execDML("create table SWIChekLost(CaseID Integer,CodeID Integer,Vol REAL,EquType TEXT,GenName TEXT,DATExist TEXT,SWIExist TEXT,SWILine int,Remark TEXT);");
        db.execDML("delete from SWIChekLost");
        if ((!db.tableExists("DATGenInfo")))
            db.execDML("create table DATGenInfo(GenName TEXT , GenVol REAL, GenCap REAL, DATLine int);");
        db.execDML("delete from DATGenInfo");
        //////////////开始从数据库中读取SWI发电机信息//////////////
        cout << "读取SWI中设备信息" << endl;
        bufSQL.format(" select * from SWIGen where  CaseID = (%d) group by GenNameData;", SWICaseID);
        SqliteHelperTable SWIGenList = db.getTable(bufSQL);
        for (int i = 0; i < SWIGenList.numRows(); i++)
        {
            SWIGenList.setRow(i);
            GenUnitDataTemp.Exist = 1;
            GenUnitDataTemp.GenID = SWIGenList.getTypeField<int>(1);
            GenUnitDataTemp.GenNameData = SWIGenList.getStringField(2);
            GenUnitDataTemp.GenVolData = SWIGenList.getTypeField<double>(3);
            GenUnitDataTemp.GenVolumData = SWIGenList.getTypeField<double>(4);
            GenUnitDataTemp.GenUnitID = SWIGenList.getTypeField<int>(5);
            GenUnitDataTemp.GenInfo = SWIGenList.getStringField(6);
            GenUnitDataVectorList.push_back(GenUnitDataTemp);
        }
        cout << "读取DAT中设备信息" << endl;
        bufSQL.format(" select a.[BusType],a.[BusName],a.[BusVol], b.[PGeMax] from Buslist a,BusNormalList b where a.[BusID] = b.[BusID] and a.[CaseID] =(%d) and (a.[BusType] = 'BQ' or a.[BusType] = 'BE' or a.[BusType] = 'BS' ) group by a.[BusID];", DATCaseID);//or a.[BusType] = 'BE'
        SqliteHelperTable DATGenInfoList = db.getTable(bufSQL);
        for (int i = 0; i < DATGenInfoList.numRows(); i++)
        {
            DATGenInfoList.setRow(i);
            DATGenCheckTemp.GenName = DATGenInfoList.getStringField(1);
            DATGenCheckTemp.GenVol = DATGenInfoList.getTypeField<double>(2);
            DATGenCheckTemp.GenCap = DATGenInfoList.getTypeField<double>(3);
            DATGenCheckTemp.Exist = 1;
            DATGenCheckInfo.push_back(DATGenCheckTemp);
        }
        cout << "容量和缺失项校验" << endl;
        /////////校验发电机容量和缺失情况////////
        for (unsigned int i = 0; i < DATGenCheckInfo.size(); i++)
        {
            for (unsigned int j = 0; j < GenUnitDataVectorList.size(); j++)
            {
                char DATGenName[50] = "";
                char SWIGenName[50] = "";
                strcpy_s(DATGenName, DATGenCheckInfo[i].GenName.c_str());
                strcpy_s(SWIGenName, GenUnitDataVectorList[j].GenNameData.c_str());
                if (strstr(DATGenName, SWIGenName) || strstr(SWIGenName, DATGenName))
                {
                    ////////////校验容量///////////
                    double CapDelt = abs(DATGenCheckInfo[i].GenCap - GenUnitDataVectorList[j].GenVolumData);
                    CommonGenCapCheckTemp.GenName = DATGenCheckInfo[i].GenName;
                    CommonGenCapCheckTemp.GenVol = DATGenCheckInfo[i].GenVol;
                    CommonGenCapCheckTemp.DATGenCap = DATGenCheckInfo[i].GenCap;
                    CommonGenCapCheckTemp.SWIGenCap = GenUnitDataVectorList[j].GenVolumData;
                    CommonGenCapCheckTemp.CapDelt = CapDelt;
                    //CommonGenCapCheckTemp.DATLine=DATGenCheckInfo[i].DATLine;
                    CommonGenCapCheckTemp.SWILine = GenUnitDataVectorList[j].GenUnitID;
                    CommonGenCapCheckInfo.push_back(CommonGenCapCheckTemp);
                    //////////校验发电机是否完备情况////////////
                    DATGenCheckInfo[i].Exist = 2;
                    GenUnitDataVectorList[j].Exist = 2;
                    break;
                }
            }
        }
        db.execDML("begin transaction;");
        ////////写入DAT中的发电机信息/////////
        for (unsigned int i = 0; i < DATGenCheckInfo.size(); i++)
        {
            sprintf_s(buf, "insert into DATGenInfo(GenName  , GenVol , GenCap , DATLine ) values('%s','%f','%f',%d);",
                DATGenCheckInfo[i].GenName.c_str(), DATGenCheckInfo[i].GenVol, DATGenCheckInfo[i].GenCap, DATGenCheckInfo[i].DATLine);
            db.execDML(buf);
        }
        ////////写入容量校验数据///////

        char buffer[20];

        for (unsigned int i = 0; i < CommonGenCapCheckInfo.size(); i++)
        {
            if (CommonGenCapCheckInfo[i].DATGenCap == 999999 && CommonGenCapCheckInfo[i].SWIGenCap != 0)
            {
                sprintf_s(buffer, "%.1f", CommonGenCapCheckInfo[i].SWIGenCap);
                string SWIGenCap_str = buffer;
                bufSQL.format("insert into SWIChekCap(CaseID ,CodeID ,Vol ,GenName ,DATCap ,SWICap ,CapDelt  ,SWILine ,Remark ) values('%d','%d',%f,'%s','%s','%s','%s',%d,'%s');",
                    SWICaseID, i + 1, CommonGenCapCheckInfo[i].GenVol, CommonGenCapCheckInfo[i].GenName.c_str(), Miss, SWIGenCap_str.c_str(), SWIGenCap_str.c_str(), CommonGenCapCheckInfo[i].SWILine, DATRemark_UnComplet);
                db.execDML(bufSQL);
            }
            if (CommonGenCapCheckInfo[i].DATGenCap != 999999 && CommonGenCapCheckInfo[i].SWIGenCap == 0)
            {
                sprintf_s(buffer, "%.1f", CommonGenCapCheckInfo[i].DATGenCap);
                string DATGenCap_str = buffer;
                bufSQL.format("insert into SWIChekCap(CaseID ,CodeID ,Vol ,GenName ,DATCap ,SWICap ,CapDelt  ,SWILine ,Remark ) values('%d','%d',%f,'%s','%s','%s','%s',%d,'%s');",
                    SWICaseID, i + 1, CommonGenCapCheckInfo[i].GenVol, CommonGenCapCheckInfo[i].GenName.c_str(), DATGenCap_str.c_str(), Miss, DATGenCap_str.c_str(), CommonGenCapCheckInfo[i].SWILine, SWIRemark_UnComplet);
                db.execDML(bufSQL);
            }
            if (CommonGenCapCheckInfo[i].DATGenCap == 999999 && CommonGenCapCheckInfo[i].SWIGenCap == 0)
            {
                bufSQL.format("insert into SWIChekCap(CaseID ,CodeID ,Vol ,GenName ,DATCap ,SWICap ,CapDelt  ,SWILine ,Remark ) values('%d','%d',%f,'%s','%s','%s','%s',%d,'%s');",
                    SWICaseID, i + 1, CommonGenCapCheckInfo[i].GenVol, CommonGenCapCheckInfo[i].GenName.c_str(), Miss, Miss, Miss, CommonGenCapCheckInfo[i].SWILine, DAT_SWIRemark_UnComplet);
                db.execDML(bufSQL);
            }
            if (CommonGenCapCheckInfo[i].DATGenCap != 999999 && CommonGenCapCheckInfo[i].SWIGenCap != 0 && CommonGenCapCheckInfo[i].CapDelt != 0)
            {
                sprintf_s(buffer, "%.1f", CommonGenCapCheckInfo[i].DATGenCap);
                string DATGenCap_str = buffer;
                sprintf_s(buffer, "%.1f", CommonGenCapCheckInfo[i].SWIGenCap);
                string SWIGenCap_str = buffer;
                sprintf_s(buffer, "%.1f", CommonGenCapCheckInfo[i].CapDelt);
                string CapDelt_str = buffer;
                bufSQL.format("insert into SWIChekCap(CaseID ,CodeID ,Vol ,GenName ,DATCap ,SWICap ,CapDelt  ,SWILine ,Remark ) values('%d','%d',%f,'%s','%s','%s','%s',%d,'%s');",
                    SWICaseID, i + 1, CommonGenCapCheckInfo[i].GenVol, CommonGenCapCheckInfo[i].GenName.c_str(), DATGenCap_str.c_str(), SWIGenCap_str.c_str(), CapDelt_str.c_str(), CommonGenCapCheckInfo[i].SWILine, CapDiff);
                db.execDML(bufSQL);
            }
            //if(CommonGenCapCheckInfo[i].CapDelt==0)
            //{
            //	sprintf_s(buf, "insert into SWIChekCap(CaseID ,CodeID ,Vol ,GenName ,DATCap ,SWICap ,CapDelt  ,SWILine ,Remark ) values('%d','%d',%f,'%s','%f','%f','%f',%d,'%s');",
            //		SWICaseID,i+1,CommonGenCapCheckInfo[i].GenVol,CommonGenCapCheckInfo[i].GenName.c_str(),CommonGenCapCheckInfo[i].DATGenCap,CommonGenCapCheckInfo[i].SWIGenCap,CommonGenCapCheckInfo[i].CapDelt,CommonGenCapCheckInfo[i].SWILine,CapSame);
            //	db.execDML(buf);
            //}
        }
        ////////写入缺失项校验////////
        cout << "写入DAT缺失项校验" << endl;
        int Num2 = 1;
        for (unsigned int i = 0; i < DATGenCheckInfo.size(); i++)
        {
            if (DATGenCheckInfo[i].Exist == 1)
            {
                sprintf_s(buf, "insert into SWIChekLost(CaseID ,CodeID ,Vol ,EquType ,GenName,DATExist ,SWIExist ,SWILine ,Remark ) values('%d','%d',%f,'%s','%s','%s','%s',%d,'%s');",
                    SWICaseID, Num2, DATGenCheckInfo[i].GenVol, GenType, DATGenCheckInfo[i].GenName.c_str(), ExistFlag, NotExistFlag, DATGenCheckInfo[i].DATLine, DATRemark_NotExist);
                db.execDML(buf);
                Num2++;
            }
        }
        //cout<<"写入SWI缺失项校验"<<endl;
        //int Num1=Num2;
        //for (unsigned int i=0;i<GenUnitDataVectorList.size();i++)
        //{
        //	if (GenUnitDataVectorList[i].Exist==1)
        //	{
        //		sprintf_s(buf, "insert into SWIChekLost(CaseID ,CodeID ,Vol ,EquType ,GenName,DATExist ,SWIExist ,SWILine ,Remark ) values('%d','%d',%f,'%s','%s','%s','%s',%d,'%s');",
        //			SWICaseID,Num1,GenUnitDataVectorList[i].GenVolData,GenType,GenUnitDataVectorList[i].GenNameData.c_str(),NotExistFlag,ExistFlag,GenUnitDataVectorList[i].GenUnitID,SWIRemark);
        //		db.execDML(buf);
        //		Num1++;
        //	}
        //}
        db.execDML("commit transaction;");
        db.close();

    }

}
